package com.online.music.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.online.music.common.Result;
import com.online.music.entity.vo.CommentVo;
import com.online.music.utils.UserContextUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import com.online.music.service.ICommentService;
import com.online.music.entity.po.Comment;

import org.springframework.web.bind.annotation.RestController;


/**
 * @Author: Leno
 * @Date: 2025-01-31
 * @Description: comment接口
 */
@RequiredArgsConstructor
@Slf4j
@Tag(name = "comment接口")
@RestController
@RequestMapping("/comment")
public class CommentController {

    private final ICommentService commentService;

    @Operation(summary = "查询所有Comment")
    @GetMapping
    public Result<List<Comment>> findAll() {
        return Result.success(commentService.list());
    }

    @Operation(summary = "根据id查询Comment")
    @GetMapping("/{id}")
    public Result<Comment> findOne(@PathVariable Long id) {
        return Result.success(commentService.getById(id));
    }

    @Operation(summary = "根据分页查询Comment")
    @GetMapping("/page")
    public Result<Page<CommentVo>> findPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize,
                                            Comment comment) {
        return commentService.findPage(pageNum, pageSize, comment);
    }

    @Operation(summary = "添加或修改Comment")
    @PostMapping
    public Result<Boolean> save(@RequestBody Comment comment) {
        comment.setUserId(UserContextUtil.getUserId());
        return Result.success(commentService.saveOrUpdate(comment));
    }

    @Operation(summary = "根据id批量删除Comment")
    @DeleteMapping("/del/batch")
    public Result<String> deleteBatch(@RequestBody List<Long> ids) {
        return commentService.deleteBatch(ids);
    }

    @Operation(summary = "根据歌词id查询评论")
    @GetMapping("/song/{id}")
    public Result<List<Comment>> findBySongId(@PathVariable Long id) {
        return Result.success(commentService.lambdaQuery().in(Comment::getSongId, id).list());
    }

}
